@using Microsoft.AspNet.Identity
@model GoblinFreelancer.Models.ManageUserViewModel

<form method="post" action="Account/Manage" id="password-form">
    @Html.AntiForgeryToken()
    @Html.ValidationSummary()
    @if(ViewBag.Success != null)
    {
        <p style="color:green">@ViewBag.Success</p>

    }
    <table>
        <tfoot>
            <tr>
                <td colspan="2">
                    <input type="submit" value="Change password" id="password-submit" />
                </td>
            </tr>
        </tfoot>
        <tbody>
            <tr>
                <td>@Html.LabelFor(m => m.OldPassword, new { @class = "control-label" })</td>
                <td>@Html.PasswordFor(m => m.OldPassword)</td>
            </tr>
            <tr>
                <td>@Html.LabelFor(m => m.NewPassword, new { @class = "control-label" })</td>
                <td>@Html.PasswordFor(m => m.NewPassword)</td>
            </tr>
            <tr>
                <td>@Html.LabelFor(m => m.ConfirmPassword, new { @class = "control-label" })</td>
                <td>@Html.PasswordFor(m => m.ConfirmPassword)</td>
            </tr>
        </tbody>
    </table>
</form>

<script>
    $('#password-submit').on('click', function (evt) {
        evt.preventDefault();
        $.post('/Account/Manage', getFormResult('password-form'), function (s) {
            $('#password-form').html(s);
        });
    });

    function getFormResult(root) {
        var form = document.getElementById(root);

        var formDataString = '';

        var inputs = form.getElementsByTagName('input');
        for (var i = 0; i < inputs.length; i++) {
            if (inputs[i].getAttribute('type') == 'radio' && !inputs[i].checked) {
                continue;
            }

            formDataString += encodeURIComponent(inputs[i].name) + '=' + encodeURIComponent(inputs[i].value) + '&';
        }

        var selects = form.getElementsByTagName('select');
        for (var i = 0; i < selects.length; i++) {
            formDataString += encodeURIComponent(selects[i].name) + '=' + encodeURIComponent(selects[i].value) + '&';
        }

        formDataString = formDataString.substring(0, formDataString.length - 1);

        return formDataString;
    }
</script>
